package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
public class jdbcconfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String name;
    @Value("${jdbc.password}")
    private String password;
    @Bean
    @Scope("prototype")
    public QueryRunner create(@Qualifier("dataSource1") DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    @Bean
    public DataSource dataSource0(){
        ComboPooledDataSource ds=new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        ds.setJdbcUrl(url);
        ds.setUser(name);
        ds.setPassword(password);
        return ds;
    }
    @Bean
    public DataSource dataSource1(){
        ComboPooledDataSource ds=new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        ds.setJdbcUrl(url);
        ds.setUser(name);
        ds.setPassword(password);
        return ds;
    }
}
